package service

import (
	"fmt"
	"ksd-social-api/commons/base/service"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/msg/mapper"
	"ksd-social-api/modules/msg/model"
	"ksd-social-api/modules/msg/model/context"
	"ksd-social-api/utils"
	"time"
)

type MessageMeService struct {
	service.BaseService
}

var messageMeMapper = mapper.MessageMeMapper{}

/**
 * 保存我的消息---注册消息
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeBindWeixin(ctx *context.MessageMeContext) bool {
	var model model.MessageMe
	utils.CopyProperties(&model, ctx)
	model.Msg = fmt.Sprintf("用户【%s】您好，你以成功绑定微信登录，后续可以使用微信扫一扫快捷登录。祝学习愉快，快乐成长。", ctx.Username)
	model.Mark = 0
	model.Mtype = 1
	model.SystemId = ctx.SystemId
	model.Url = "https://www.study512.com"
	model.Opid = ctx.UserId
	model.Title = "绑定微信"
	model.Cover = ""
	model.Description = "绑定微信"
	return messageMeMapper.SaveMessageMe(&model)
}

/**
 * 保存我的消息---注册消息
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeReg(ctx *context.MessageMeContext) bool {
	var model model.MessageMe
	utils.CopyProperties(&model, ctx)
	model.Msg = fmt.Sprintf("恭喜【%s】,成为本平台的注册用户，祝学习愉快，快乐成长。", ctx.Username)
	model.Mark = 0
	model.Mtype = 1
	model.SystemId = ctx.SystemId
	model.Url = "https://www.study512.com"
	model.Opid = ctx.UserId
	model.Title = "新用户注册"
	model.Cover = ""
	model.Description = "新用户注册"
	return messageMeMapper.SaveMessageMe(&model)
}

/**
 * 保存我的消息---vip升级
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeVIP(ctx *context.MessageMeContext) bool {
	var model model.MessageMe
	utils.CopyProperties(&model, ctx)
	model.Msg = fmt.Sprintf("恭喜%s,升级成为【%s】，祝学习愉快，快乐成长。", ctx.Username, ctx.VipName)
	model.Mark = 0
	model.Mtype = 2
	model.Url = "javascript:void(0);"
	model.Opid = 0
	model.Title = "会员用户升级"
	model.Cover = ""
	model.SystemId = ctx.SystemId
	model.Description = ""
	return messageMeMapper.SaveMessageMe(&model)
}

/**
 * 保存我的消息---购买课程
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeBuyNote(ctx *context.MessageMeContext) bool {
	if messageMeMapper.CountMessageMeByOpid(3, ctx.Opid, ctx.UserId, ctx.SystemId) {
		var model model.MessageMe
		utils.CopyProperties(&model, ctx)
		model.Msg = fmt.Sprintf("购买课程【%s】成功,点击标题或者详情前往学习！", ctx.Title)
		model.Mark = 0
		model.Mtype = 3
		model.SystemId = ctx.SystemId
		model.Title = "购买课程"
		return messageMeMapper.SaveMessageMe(&model)
	} else {
		return false
	}
}

/**
 * 保存我的消息---购买课程
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeBuyClassRoom(ctx *context.MessageMeContext) bool {
	if messageMeMapper.CountMessageMeByOpid(3, ctx.Opid, ctx.UserId, ctx.SystemId) {
		var model model.MessageMe
		utils.CopyProperties(&model, ctx)
		model.Msg = fmt.Sprintf("购买班级【%s】成功,点击标题或者详情前往学习！", ctx.Title)
		model.Mark = 0
		model.Mtype = 11
		model.SystemId = ctx.SystemId
		model.Title = "购买班级"
		return messageMeMapper.SaveMessageMe(&model)
	} else {
		return false
	}
}

/**
 * 保存我的消息---购买专题
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeBuySpecial(ctx *context.MessageMeContext) bool {
	if messageMeMapper.CountMessageMeByOpid(4, ctx.Opid, ctx.UserId, ctx.SystemId) {
		var model model.MessageMe
		utils.CopyProperties(&model, ctx)
		model.Msg = fmt.Sprintf("购买专题【%s】成功,点击标题或者详情前往学习！", ctx.Title)
		model.Mark = 0
		model.Mtype = 4
		model.SystemId = ctx.SystemId
		model.Title = "购买专题"
		return messageMeMapper.SaveMessageMe(&model)
	} else {
		return false
	}
}

/**
 * 保存我的消息---购买栏目
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeChannel(ctx *context.MessageMeContext) bool {
	if messageMeMapper.CountMessageMeByUserId(5, ctx.UserId, ctx.SystemId) {
		var model model.MessageMe
		utils.CopyProperties(&model, ctx)
		model.Msg = fmt.Sprintf("购买栏目【%s】成功", ctx.Title)
		model.Mark = 0
		model.Mtype = 5
		model.Title = "购买栏目"
		model.Description = "web"
		model.SystemId = ctx.SystemId
		return messageMeMapper.SaveMessageMe(&model)
	} else {
		return false
	}
}

/**
 * 保存我的消息---登陆通知
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeUserLogin(ctx *context.MessageMeContext) bool {
	var model model.MessageMe
	utils.CopyProperties(&model, ctx)
	model.Msg = fmt.Sprintf("用户【%s】您好，你于【%v】时间登陆学习平台！", ctx.Username, time.Now())
	model.Mark = 0
	model.Mtype = 6
	model.Title = "登陆通知"
	model.Description = "web"
	model.SystemId = ctx.SystemId
	return messageMeMapper.SaveMessageMe(&model)
}

/**
 * 保存我的消息---购买课程
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeBuyResume(ctx *context.MessageMeContext) bool {
	if messageMeMapper.CountMessageMeByUserId(7, ctx.UserId, ctx.SystemId) {
		var model model.MessageMe
		utils.CopyProperties(&model, ctx)
		model.Msg = fmt.Sprintf("购买简历服务【%s】成功,同时赠送3个月平台VIP", ctx.Title)
		model.Mark = 0
		model.Mtype = 7
		model.SystemId = ctx.SystemId
		model.Title = "购买简历服务"
		return messageMeMapper.SaveMessageMe(&model)
	} else {
		return false
	}
}

/**
 * 保存我的消息---购买课程
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeBuyResumeTemplate(ctx *context.MessageMeContext) bool {
	if messageMeMapper.CountMessageMeByOpid(8, ctx.Opid, ctx.UserId, ctx.SystemId) {
		var model model.MessageMe
		utils.CopyProperties(&model, ctx)
		model.Msg = fmt.Sprintf("购买简历模板【%s】成功！", ctx.Title)
		model.Mark = 0
		model.Mtype = 8
		model.SystemId = ctx.SystemId
		model.Title = "购买简历模板"
		return messageMeMapper.SaveMessageMe(&model)
	} else {
		return false
	}
}

/**
 * 保存我的消息---充值
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) SaveMessageMeUserCoin(ctx *context.MessageMeContext) bool {
	var model model.MessageMe
	utils.CopyProperties(&model, ctx)
	model.Msg = fmt.Sprintf("用户【%s】您好，充值学习币成功，获取学习币：【%s】个", ctx.Username, ctx.Coin)
	model.Mark = 0
	model.Mtype = 9
	model.Title = "用户充值学习币"
	model.Description = "coin"
	model.SystemId = ctx.SystemId
	return messageMeMapper.SaveMessageMe(&model)
}

/**
 * 根据ID删除文章评论消息
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) DelMessageMeById(id int, userId uint64, systemId uint64) bool {
	return messageMeMapper.DelMessageMeById(id, userId, systemId)
}

/**
 * 文章评论已读和未读
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) UpdateMessageMeMarkById(id int, userId uint64, systemId uint64) bool {
	return messageMeMapper.UpdateMessageMeMarkById(id, userId, systemId, 1)
}

/**
 * 一键已读
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (service *MessageMeService) UpdateMessageMeMarkByUserId(userId uint64, systemId uint64) bool {
	return messageMeMapper.UpdateMessageMeMarkByUserId(userId, systemId, 1)
}

/**
 * @desc 根据ID查询我的消息
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (service *MessageMeService) GetMessageMeById(id int, userId uint64, systemId uint64) (*model.MessageMe, error) {
	return messageMeMapper.GetMessageMeById(id, userId, systemId)
}

/*
*
查询用户消息
*/
func (service *MessageMeService) FindMessageMePage(userId uint64, systemId uint64, pageNo int64, pageSize int64) (p *page.Page, err error) {
	return messageMeMapper.FindMessageMePage(userId, systemId, 1, 100)
}
